package com.xbongbong.paas.config;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.service.workflow.setting.ConditionNodeFieldService;
import com.xbongbong.paas.service.workflow.setting.WorkflowNodeSettings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 吴峰
 * @date 2021/05/21 14:38
 */
@Configuration
public class WorkflowNodeSettingsConfig {

    @Autowired
    @Lazy
    private Map<String, WorkflowNodeSettings> workflowNodeSettingsMap;

    @Autowired
    private Map<String, ConditionNodeFieldService> conditionNodeFieldMap;

    @Bean("workflowNodeSettingsMap")
    public Map<Integer, WorkflowNodeSettings> initWorkflowNodeSettingsMap() {
        return workflowNodeSettingsMap.values().stream().collect(Collectors.toMap(WorkflowNodeSettings::type, t -> t));
    }

    @Bean("conditionNodeFieldMap")
    public Map<Integer, ConditionNodeFieldService> initConditionNodeFieldMap() {
        Map<Integer, ConditionNodeFieldService> conditionNodeFieldServiceMap = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        conditionNodeFieldMap.forEach((key, item)->{
            item.type().forEach(fieldType->{
                conditionNodeFieldServiceMap.put(fieldType, item);
            });
        });

        return conditionNodeFieldServiceMap;
    }
}
